package com.service;

import com.dao.MenuDao;
import com.dao.RoleDao;
import com.dao.RoleMenuDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.util.AjaxResult;
import com.util.UtilDate;
import com.util.UtilRandom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class RoleService {

    @Autowired
    RoleDao roleDao;

    @Autowired
    RoleMenuDao roleMenuDao;

    @Autowired
    MenuDao menuDao;

    public AjaxResult save(String user_id,String user_name,String role_name,String remark){
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("role_id", UtilRandom.uuid());
            param.put("role_name",role_name);
            param.put("remark",remark);
            param.put("create_user_id",user_id);
            param.put("create_user_name",user_name);
            param.put("create_time", UtilDate.now());
            param.put("update_user_id",user_id);
            param.put("update_user_name",user_name);
            param.put("update_time", UtilDate.now());
            int ck = roleDao.save(param);
            if(ck > 0){
                return AjaxResult.getOk();
            }
        } catch (Exception e) {
            if(e.getMessage().contains("Duplicate entry")){
                return AjaxResult.getFail("角色【"+role_name+"】已存在");
            }
            return AjaxResult.getFail(e.getMessage());
        }
        return AjaxResult.getFail();
    }

    public AjaxResult update(String user_id,String user_name,String role_id,String role_name,String remark){
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("role_id",role_id);
            param.put("role_name",role_name);
            param.put("remark",remark);
            param.put("update_user_id",user_id);
            param.put("update_user_name",user_name);
            param.put("update_time", UtilDate.now());
            int ck = roleDao.update(param);
            if(ck > 0){
                return AjaxResult.getOk();
            }
        } catch (Exception e) {
            if(e.getMessage().contains("Duplicate entry")){
                return AjaxResult.getFail("角色【"+role_name+"】已存在");
            }
            return AjaxResult.getFail(e.getMessage());
        }
        return AjaxResult.getFail();
    }

    public AjaxResult del(String role_id) {
        try {
            int ck = roleDao.del(role_id);
            if(ck > 0){
                return AjaxResult.getOk();
            }
        } catch (Exception e) {
            return AjaxResult.getFail(e.getMessage());
        }
        return AjaxResult.getFail();
    }

    public AjaxResult list(String role_name, String pageNum, String pageSize) {
        PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
        List<Map<String,Object>> list = roleDao.list(role_name);
        return AjaxResult.getOk(new PageInfo<>(list));
    }

    public AjaxResult configMenus(String role_id, List<String> list) {
        roleMenuDao.delByRoleId(role_id);
        boolean flag;
        List<String> listPid = new ArrayList<>();
        listPid.addAll(list);
        //查询全部上级菜单
        do{
            //查询指定菜单上级菜单
            List<String> list2 = menuDao.listPidByListId(listPid);
            if(list2.isEmpty()||list2.contains("0")){
                flag = false;
                break;
            }
            flag = true;
            listPid.clear();
            listPid.addAll(list2);
            list.addAll(list2);
        }while (flag);
        roleMenuDao.batAdd(role_id,list);
        return AjaxResult.getOk();
    }

    public AjaxResult listMenuId(String role_id) {
        List<String> list = roleMenuDao.listByRoleId(role_id);
        return AjaxResult.getOk(list);
    }

    public AjaxResult listAllSelect() {
        List<Map<String,Object>> list = roleDao.listAllSelect();
        return AjaxResult.getOk(list);
    }
}
